<template>
  <div class="wrap">
    <div v-if="arr.length == 0" class="noData">
      <img src="./../../assets/img/noData.png" alt="" />
      <div class="txt">暂无携行用户</div>
    </div>
    <div v-else class="ul">
      <van-swipe-cell
        :right-width="rightWidth"
        class="message-li"
        v-for="(item, index) in arr"
        :key="index"
        :id="item.user_id"
      >
        <div class="content" @click="goHerf(item.user_id)">
          <div class="li-left">
            <img :src="item.face_photo ? item.face_photo : avatar_default" />
          </div>
          <div class="li-right">
            <div class="box">
              <div class="title">昵称：</div>
              <div class="txt">{{ item.nick_name }}</div>
            </div>
            <div class="box">
              <div class="title">年龄：</div>
              <div class="txt">{{ item.age }}岁</div>
            </div>
            <div class="box">
              <div class="title">性别：</div>
              <div class="txt">{{ item.gender == 1 ? '男' : '女' }}</div>
            </div>
          </div>
        </div>
        <template #right>
          <van-button
            square
            type="danger"
            text="删除"
            :stop-propagation="true"
            @click="onClose(item.user_id)"
          />
        </template>
      </van-swipe-cell>
    </div>
    <div class="btn-wrap">
      <button class="btn" @click="goCarryUserRegister">创建携行用户</button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { showSuccessToast, showFailToast } from 'vant'
import { postData } from './../../request/request.js'
import { useRoute } from 'vue-router'
let route = useRoute()
let query = route.query
localStorage.clear()
localStorage.setItem('user_id', query.user_id || '')
localStorage.setItem('access_token', query.access_token || '')
localStorage.setItem('open_id', query.open_id || '')
localStorage.setItem('currentPath', query.currentPath || '')

const avatar_default = ref(
  'https://ya30images.hbwlife.com/wxmp/default-pic.png'
)
const arr = ref([])
const rightWidth = ref(60)

const initList = () => {
  postData('/user/carried/list', {
    with_health: 1,
  })
    .then((res) => {
      arr.value = res?.datas?.users || []
    })
    .catch((err) => {
      console.log(err)
    })
}

const goCarryUserRegister = () => {
  wx &&
    wx.miniProgram.navigateTo({
      url: '/pagesC/carryUserRegister/index',
    })
}

const goHerf = (carriedUserid) => {
  wx &&
    wx.miniProgram.navigateTo({
      url: '/pagesC/carryUserDetail/index?carriedUserid=' + carriedUserid,
    })
}

//左滑-删除-关闭
let onClose = (carriedUserid) => {
  postData('/user/carried/cancel', {
    carried_user_id: carriedUserid,
  })
    .then((res) => {
      if (res.code == 0) {
        showSuccessToast('已删除')
        initList()
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

onMounted(() => {
  initList()
})

watch(
  () => route.fullPath,
  () => {
    initList()
  }
)
</script>

<style scoped>
.wrap {
  width: 100%;
  height: 100vh;
  overflow-y: auto;
  background: #f5f5f5;
}
.noData {
  display: flex;
  flex-flow: column;
  justify-content: flex-start;
  align-items: center;
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
}
.noData img {
  width: 0.4rem;
  height: 0.4rem;
}
.noData .txt {
  margin-top: 0.1rem;
  font-size: 0.14rem;
  color: #8d8f91;
}
.ul {
  width: 100%;
  height: auto;
  margin: 0;
  padding: 0;
  display: flex;
  flex-flow: column;
  justify-content: flex-start;
  align-items: center;
  font-size: 0.16rem;
  color: #333;
}
.van-swipe-cell {
  width: 100%;
  height: 100%;
}
::v-deep .van-swipe-cell__wrapper {
  width: 100%;
  height: 100% !important;
}

.message-li {
  height: 1.3rem;
  background: #fff;
  margin: 0.08rem 0;
  width: 95%;
  box-sizing: border-box;
  position: relative;
  box-shadow: 0 0.1rem 0.1rem #d6d6d6;
  border-radius: 0.1rem;
}
.content {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  height: 100%;
  padding: 0.1rem 0.2rem;
}
.li-left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.li-left img {
  width: 0.6rem;
  height: 0.6rem;
}
.li-right {
  height: 100%;
  flex: 1;
  box-sizing: border-box;
  display: flex;
  flex-flow: column;
  justify-content: center;
  align-items: flex-start;
  margin-left: 0.2rem;
}
.li-right .box {
  height: 25%;
  line-height: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 0.16rem;
}
.ul .content .li-right .box .txt {
  margin-left: 0.075rem;
}
/* .van-swipe-cell__right{
   height: 1.3rem !important;
} */
van-swipe-cell__right .van-swipe-cell__right {
  height: 100% !important;
}
.btn-wrap {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 0.5rem;
}
.btn {
  width: 95%;
  height: 0.4rem;
  border-radius: 0.08rem;
  color: #533504;
  background: linear-gradient(to right, #c8a357, #fbde94, #c8a357);
  margin: 0 auto;
  outline: none;
  border: none;
  font-size: 0.16rem;
}
.add-btn {
  border: none;
  width: 60%;
  height: 0.4rem;
  line-height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #d5ad57;
  border-radius: 0.2rem;
  color: #fff;
  font-size: 0.16rem;
  font-weight: 400;
}
</style>
